<?php
// $objResponse->addAlert('Response: I am here!');return $objResponse;
class Name extends Controller 
{
	var $uid;
	var $tpl;

	function setVars()
	{
		$this->uid = (object) $this->session->userdata('uid');
	}
	
	function Name()
	{
		parent::Controller();
		$this->setVars();
		$this->output->enable_profiler(TRUE);
		log_message('debug', "Name Controler Class Initialized");
	}

	function Menu()
	{
		$menu[] = anchor('/','Home');
		return $menu;
	}

/*****************************************************************\
|               Register User Name
\*****************************************************************/	

	function index() 
	{
		$this->tpl['Messanger'] = 'No New Messages';
		$this->tpl['jsOnLoad']  = '';
		$this->tpl['title'] = "Name";
		$this->tpl['head'] = '';
		$this->tpl['menu'] = $this->Menu();
		$this->tpl['body'] = '';
		$this->tpl['countryOptions'] = $this->country_options();
		$sq = $this->security_options();
		$this->tpl['securityQuestions'] = $sq['Questions'];
		$this->tpl['securityAnswers'] = $sq['Answers'];
		$this->load->library('timetamer');
		$this->tpl['dtsform'] = $this->timetamer->MakeForm();
		$this->tpl['captcha'] = $this->captcha_view();

		
		
		
		
		
		
		
		
		
		
		if(isset($_POST['name']))
		{
			$this->postCheck();
		}
		else
		{
			$this->load->view('account/name', $this->tpl);
		}
	}

	function postCheck()
	{
		$this->load->library('validation');
		$rules['First_Name'] = "trim|required|min_length[2]|max_length[100]|xss_clean";
		$rules['Last_Name']	= "trim|required|min_length[2]|max_length[100]|xss_clean";
		$rules['Country']		= "trim|required|min_length[2]|max_length[2]|xss_clean";
		$this->validation->set_rules($rules);

		if ($this->validation->run() == FALSE)
		{
			$msg = $this->validation->error_string;
			$this->msg->setError($msg);
			$msg = $this->msg->getMsg();
			$this->tpl['jsOnLoad'] = $msg->jsOnLoad;
			$this->tpl['Messanger'] = $msg->html;
			$this->load->view('account/name', $this->tpl);
		}
	}
/*
		if ($this->validation->run() !== FALSE)
		{
			$First_Name = $this->input->post('First_Name');
			$Last_Name = $this->input->post('Last_Name');
			$uid = $this->session->userdata('uid');
			$data = array('First_Name' => $First_Name, 'Last_Name' => $Last_Name);
			$this->db->where('uid', $uid);
			$this->db->update('Account', $data);
			
			$this->msg->setMsg('Your Name has been updated.');
			$_POST = $tmppost;
			$msg = $this->msg->getMsg();
			$objResponse->addAssign("ContentDiv","innerHTML",$this->axContent('other'));
			$objResponse->addAssign("MessageDiv","innerHTML",$msg->html);
			$objResponse->addScript("document.getElementById('MessageDiv').style.display='inline'; 
				setTimeout(\"document.getElementById('MessageDiv').style.display='none'\",$msg->time); ");
		}else{
			$msg = $this->validation->error_string;
			$_POST = $tmppost;
			$this->msg->setError($msg);
			$msg = $this->msg->getMsg();
			$objResponse->addAssign("MessageDiv","innerHTML",$msg->html);
			$objResponse->addScript("document.getElementById('MessageDiv').style.display='inline'; 
				setTimeout(\"document.getElementById('MessageDiv').style.display='none'\",$msg->time); ");
		}
		$_POST = $tmppost;

	
*/

	
   function country_options()
	{
		$this->db->select('FIPS, Country')->from('Country');
      $query = $this->db->get();
		$selected = 'US';
		$output = '<select name="Country">'; // multiple size=\"$size\"
        foreach ($query->result() as $row ) {
            $output .= "<option value=\"$row->FIPS\"";
            if ( $row->FIPS == $selected ) {
                $output .= ' selected';
            }
            $output .= ">$row->Country</option>\n";
        }

		$output .= '</select>'; 
		return $output;
	}
   	    	 
   function security_options()
	{
		$sql = "SELECT `key` , `value` , `Description`
		FROM `config`
		WHERE `what` = 'Select'
		AND `for` = 'Security Question';";
		
		$query = $this->db->query($sql);
		
		$Question = "";
		$Answer = '';
		$Question = '<select name="Question"'; 
		$Question .= "onchange=\"secShowHide(this.options[this.selectedIndex].value)\"
		onmouseover=\"secHide(this.options[this.selectedIndex].value)\">";
		$Question .= "\t<option value=\"0\">--Select--</option>\n";
		foreach($query->result() as $row) {
			$row->Description = explode(', ',$row->Description); 
			$Question .= "\t<option value=\"$row->key\">$row->value</option>\n";
			$Answer .= "<select id=\"$row->key\" style=\"display: none;\" name=\"$row->key\">\n";
			$Answer .= "\t<option value=\"0\">--Select--</option>\n";
			foreach($row->Description as $k => $v) {
				$k++;
				$Answer .= "\t<option value=\"$k\">$v</option>\n";
			}
			$Answer .= "</select>\n";
		}
		$Question .= "</select>\n";

		$return['Questions'] = $Question;
		$return['Answers'] = $Answer;
		return $return;
	}			
		
/*****************************************************************\
|               CAPTCHA
\*****************************************************************/	

	function captcha_view()
	{
		$this->load->plugin('captcha');
		$vals = array(
			'img_path'	 => '/var/www/public/captcha/',
			'img_url'	 => 'http://localhost/public/captcha/',
			'font_path'	 => '/var/www/system/fonts/jester.ttf',
			'img_width'	 => 110,
			'img_height' => 25,
			'expiration' => 360
			);
		
		$cap = create_captcha($vals);
		
		$data = array(
			'captcha_id'	=> '',
			'captcha_time'	=> $cap['time'],
			'ip_address'	=> $this->input->ip_address(),
			'word'			=> $cap['word']
			);
		
		$query = $this->db->insert_string('captcha', $data);
		$this->db->query($query);
		
		//$data  = '<br />Submit the word you see below:<br />';
		$data = $cap['image'];
		//$data .= '<br /><input type="text" name="captcha" />';
		return $data;
	}

	
	function captcha_check()
	{
			// First, delete old captchas
			$expiration = time()-1500;
			$this->db->query("DELETE FROM captcha WHERE captcha_time < ".$expiration);
			
			// Then see if a captcha exists:
			$sql = "SELECT COUNT(*) AS count FROM captcha WHERE word = ? AND ip_address = ? ";
			$binds = array($_POST['captcha'], $this->input->ip_address(), $expiration);
			$query = $this->db->query($sql, $binds);
			$row = $query->row();
			
			if ($row->count == 0)
			{
				$this->validation->set_message('captcha_check', 'You must submit the word that appears in the image.');
				return FALSE;
			}	
	}
}
?>
